# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
	include HelperTools
  
  def head_options(record, options={})
    return if record.nil?
    
    header = nil
    header = record.header if record.respond_to?(:header)
    
    # title
    page_title = []
		page_title << options[:title] if options.key?(:title)
		page_title << ((header.nil? || header.title.blank?) ? record.title : header.title)
		page_title << t('page.title') if options[:append_title]
		page_title.flatten!
		page_title.compact!
		page_title.uniq!
		content_for :title, page_title.join(" | ")
		
		# keywords
		keywords = record.keywords.join(', ') if record.respond_to?(:keywords)
		keywords ||= (header.nil? || header.keywords.blank?) ? page_title.join(' ').split.join(', ') : header.keywords
		content_for :keywords, tag("meta", :content=>keywords, :name=>"keywords")
		
		# description
		description = (header.nil? || header.description.blank?) ? page_title.join(" - ") : header.description
		content_for :description, tag("meta", :content=>description, :name=>"description")
  end
  
  def item_path(record)
    unless record.nil?
      case record.kind
        when :page then page_path(record.slug)
        when :main then root_path
        when :posts then posts_path
        when :gallery then photos_path
        when :services then services_path
        when :coaches then coaches_path
      end
    end
  end
  
  def new_attachment_path_with_session_information
    session_key = ActionController::Base.session_options[:key]
    manage_photos_url(session_key => cookies[session_key], 
    		request_forgery_protection_token => form_authenticity_token, :protocol=>"http://")
  end
end
